/**
 * 
 */
define(function(require) {
    var comm = require('sdk/server');
    require('sdk/common');
    require('box');
    var userData = comm.getUser();

    var param = appcan.ls.val('crossParam');
    if (param) {
        param = JSON.parse(param);
    } else {
        return console.log('no param for partDetail!');
    };

    var remarkValue; //备注信息用户填写内容
    var minBuyCount; //最小购买数量
    var amount = 1; //购买商品数量

    //评价
    $('body').on('click', '#toRecomm', function() {
        appcan.openView(param.id, "parts", "recommList");
    }).on('click', '#buyNow', function() {

    });

    //强制备注信息
    comm.proCounter(function(inp) {
        amount = inp.val();
    });
    //最小单元数
    var forceRemarkBox = function() {
        var remarkHTML = $('<div class="remarkBox" />');
        var remarkHTMLCatch = '';
        var minBuyCount = window.minBuyCount || 1;
        amount = minBuyCount;
        remarkHTMLCatch = '<div class="_title">' + '请输入备注信息：</div>' +
            '<div class="list"><label class="item item-input">' +
            '<input type="text" class="_inp" /></label>';

        remarkHTMLCatch += '<div class="item">购买数量\
                    <div class="r pro_counter flex-row">\
                    <button class="btn btn-default pro_counter_reduce" data-minbuycount="' + minBuyCount + '">-</button>\
                    <input type="text" value="' + minBuyCount + '" class="pro_counter_val">\
                    <button class="btn btn-default pro_counter_add" data-minbuycount="' + minBuyCount + '">+</button>\
                    </div>\
                </div></div>';
        remarkHTML.append(remarkHTMLCatch);
        $.box.alert(remarkHTML.get(0).outerHTML, function() {
            addToCar();
        }, {
            onshow: function($this) {
                //输入
                $this.find('._inp').on('blur', function() {
                    remarkValue = $(this).val();
                });
            }
        });
    };

    var addToCar = function() {
        $.box.hide();
        //alert(amount)
        appcan.ajax({
            url: appcfg.host.control + '/eshop/addToCart.jsp',
            data: {
                sid: appcfg.project.sid,
                goodsid: param.id,
                jr_member_id: userData.id,
                value: remarkValue,
                sid: appcfg.project.sid,
                amount: amount
            },
            success: function(res) {
                if (res.status === 'Y') {
                    appcan.loading.show('加入购物车成功', {
                        icon: '&#xe640;',
                        delay: 500
                    });
                } else if (res.code == 'nolog') {
                    $.box.msg(res.message, {
                        color: 'danger',
                        delay: 2000,
                        onclose: function() {
                            appcan.openView(null, 'member', 'login');
                        }
                    });
                } else if (res.code == 'fail') {
                    //提交失败
                    $.box.msg(res.message, {
                        color: 'danger',
                        delay: 2000
                    });
                }
            }
        });
    };
    //加入购物车
    $('body').on('click', '#addGouwuche', function() {
        forceRemarkBox();
    });
    //收藏
    function gotoCollection() {
        appcan.ajax({
            url: appcfg.host.control + '/goods/collectGoods.jsp',
            data: {
                sid: appcfg.project.sid,
                goods_id: param.id,
                member_id: userData.id,
                usern: userData.usern
            },
            success: function(res) {
                if (res.status === 'Y') {
                    appcan.window.openToast("收藏成功！", 2000, 5);
                    $("#addTofav").addClass("active");
                    $("#gotocollection").html("取消收藏");
                }
            }
        });

    };
    //取消收藏
    function cancleCollection() {
        appcan.ajax({
            url: appcfg.host.control + '/eshop/cancleCollectGoods.jsp',
            data: {
                sid: appcfg.project.sid,
                goods_id: param.id,
                member_id: userData.id
            },
            success: function(res) {
                if (res.status === 'Y') {
                    appcan.window.openToast("已取消收藏！", 2000, 5);
                    $("#addTofav").removeClass("active");
                    $("#gotocollection").html("加入收藏");
                }
            }
        });
    };
    //收藏
    $('body').on('click', '#addTofav', function() {
        if ($(this).hasClass('active')) {
            //取消收藏请求
            cancleCollection();
        } else {
            //发送收藏请求
            gotoCollection();
        }
    });

    var dataTemp = $('#partDetailTemp').val();
    var dataRender = function(data) {
        var render = etpl.compile(dataTemp);
        var data = data.data[0];

        window.minBuyCount = data.minBuyCount;

        var html = render({
            part: data
        });
        //替代的配件不显示收藏，加入购物车
        //console.log(data.oeNo)
        if (data.oeNo && data.oeNo != '' && data.oeNo != ' ') {
            $('.foot').addClass('hidden');
        } else {
            $('.foot').removeClass('hidden');
        }
        $('#partDetail').html(html).cacheImg();
        //来自加入购物车
        if (param.remark) {
            forceRemarkBox()
        }
        if (data.isCollect == 1) {
            $("#gotocollection").html("取消收藏");
        }
        // 图片slider
        var dataImg = data.imgList || [],
            partSlide,
            slideFunc = function() {
                partSlide = new iSlider({
                    dom: document.getElementById('Slider-wrapper'),
                    data: dataImg,
                    isLooping: true,
                    isAutoplay: false,
                    plugins: ['dot']
                });
            };

        if (dataImg.length) {
            $('#slideWrap').removeClass('hide');
            $.each(dataImg, function(i, e) {
                e.content = comm.source(e.content);
            });

            if (window.iSlider) {
                slideFunc();
            } else {
                appcan.getScript('lib/islider', function() {
                    slideFunc();
                }, {
                    css: true
                });
            }
        } else {
            $('#slideWrap').addClass('hide');
        }
        appcan.loading.hide();
    };

    var getData = function() {
        appcan.loading.show('', {
            wrap: '#View'
        });
        appcan.ajax({
            url: appcfg.host.control + '/goods/getGoodsDetail.jsp',
            data: {
                goods_id: param.id,
                member_id: userData.id
            },
            success: function(res) {
                if (res.status === 'Y') {
                    dataRender(res);
                } else if (res.status === 'N') {
                    appcan.loading.hide();
                    $('#partDetail').empty();
                    $.box.msg(res.message, {
                        color: 'danger',
                        delay: 2000
                    });
                }
            }
        });
    };

    appcan.ready(function() {
        getData();
        
    });
});